package com.campusnews.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.campusnews.annotation.LoginToken;
import com.campusnews.pojo.SchoolUser;
import com.campusnews.pojo.Thing;
import com.campusnews.pojo.dayData;
import com.campusnews.service.SchoolUserService;
import com.campusnews.utils.JsonResult;
import com.campusnews.utils.EntryToMapUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqc
 * @since 2021-09-26
 */
@RestController
@ApiOperation(value = "用户API", notes = "用户API")
@RequestMapping("/schoolUser")
@Api(tags={"用户API"})
public class SchoolUserController {
    @Autowired
    private SchoolUserService schoolUserService;

    @LoginToken
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    @RequestMapping(value = "/updateUser" ,method = RequestMethod.POST)
    public JsonResult updateUser(@RequestBody SchoolUser user) {
        UpdateWrapper<SchoolUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("ban_time",user.getBanTime())
                .set("ban_day",user.getBanDay())
                .eq("user_id",user.getUserId());
        boolean update = schoolUserService.update(updateWrapper);
        if (update){
            return JsonResult.build(200,"用户修改成功",update);
        }else {
            return JsonResult.errorMsg("用户修改失败");
        }
    }

    @LoginToken
    @ApiOperation(value = "删除用户信息", notes = "删除用户信息")
    @RequestMapping(value = "/deleteUser/{userId}" ,method = RequestMethod.GET)
    public JsonResult deleteUser(@PathVariable("userId") int userId) {
        QueryWrapper<SchoolUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        boolean remove = schoolUserService.remove(wrapper);
        if (remove){
            return JsonResult.build(200,"用户删除成功",remove);
        }else {
            return JsonResult.errorMsg("用户删除失败");
        }
    }

//    @LoginToken
    @ApiOperation(value = "分页精确查询用户信息", notes = "分页精确用户信息")
    @RequestMapping(value = "/selectUserByAccurate" ,method = RequestMethod.POST)
    public JsonResult selectUserByAccurate(@RequestBody SchoolUser user){
        Map<String, Object> map = new HashMap<>();
        try {
            map = EntryToMapUtils.entryToMap(user);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //条件查询器
        QueryWrapper<SchoolUser> wrapper = new QueryWrapper<>();
        wrapper.allEq(map);
        if (!(user.getSortOrder()==null||user.getSortOrder().length()==0)){
            wrapper.orderByDesc(user.getSortOrder());
        }
        PageHelper.startPage(user.getPage(),user.getPageSize());
        PageInfo<SchoolUser> pageInfo = new PageInfo<>((Page<SchoolUser>)schoolUserService.list(wrapper));
        System.out.println(pageInfo);
        return JsonResult.build(200,"分页精确用户信息查询成功",pageInfo);
    }

//    @LoginToken
    @ApiOperation(value = "分页模糊查询用户信息", notes = "分页模糊用户信息")
    @RequestMapping(value = "/selectUserByVague" ,method = RequestMethod.POST)
    public JsonResult selectUserByVague(@RequestBody Thing things) {
        //条件查询器
        String thing = things.getThing();
        QueryWrapper<SchoolUser> wrapper = new QueryWrapper<>();
        if (!(thing==null||thing.length()==0)){
            wrapper.like("nickname",thing).or()
                    .like("birthday",thing).or()
                    .like("city",thing).or()
                    .like("signature",thing).or();
        }
        if (!(things.getSortOrder()==null||things.getSortOrder().length()==0)){
            wrapper.orderByDesc(things.getSortOrder());
        }
        PageHelper.startPage(things.getPage(),things.getPageSize());
        PageInfo<SchoolUser> pageInfo = new PageInfo<>((Page<SchoolUser>)schoolUserService.list(wrapper));
        System.out.println(pageInfo);
        return JsonResult.build(200,"分页模糊用户信息查询成功",pageInfo);
    }

}

